package com.itheima.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/*
Stream 流的收集方法 collect()
收集到Map 中
 */
public class Demo05 {


    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(List.of("吴亦凡,31", "鹿晗,30", "蔡徐坤,15", "黄子韬,13", "张艺兴,31"));
    /*    List<String> collect = list.stream().filter(s -> {
            String[] studentInfo = s.split(",");
            String name = studentInfo[0];
            String ageStr = studentInfo[1];
            int age = Integer.parseInt(ageStr);
            return age <= 18;
        }).collect(Collectors.toList());*///收集到字符串的集合中

        //目标 把这些数据封装到 map中 姓名 作为 键   年龄作为值
        /*Map<String,Integer> map = list.stream()
                .filter(s -> Integer.parseInt(s.split(",")[1]) <= 18)
                .collect(Collectors.toMap(new Function<String, String>() {
                    @Override
                    public String apply(String s) {
                        return s.split(",")[0];
                    }
                }, new Function<String, Integer>() {
                    @Override
                    public Integer apply(String s) {
                        return Integer.parseInt(s.split(",")[1]);
                    }
                }));*/
        //获取流
        Stream<String> stream = list.stream();
        //过滤
        Stream<String> stream2 = stream.filter(s->{
            //以,切割   [0]姓名  [1] 年龄字符串
            String[] split = s.split(",");
            String ageStr = split[1];
            //将年龄字符串转换成 int
            int age = Integer.parseInt(ageStr);
            return age<=18;
        });
        //使用Map 收集
        Map<String, Integer> map = stream2.collect(Collectors.toMap(s -> s.split(",")[0], s ->  Integer.parseInt(s.split(",")[1])));

        map.forEach((k,v)->{
            System.out.println(k+"========"+v);
        });

    }
}
